package com.code.petapp.util;

import android.content.Context;
import android.util.Log;

import com.code.petapp.bean.BaseBean;
import com.code.petapp.bean.OrderBean;
import com.code.petapp.bean.UserBean;
import com.code.petapp.http.HttpApiService;
import com.code.petapp.http.HttpListener;
import com.code.petapp.http.RetrofitHelper;
import com.code.petapp.http.RxObserver;

import java.util.ArrayList;

public class UserUtils {
    public static void login( UserBean userBean, HttpListener<UserBean> httpListener){
        Log.d("Tag","Login start");
        new RetrofitHelper().createApi(HttpApiService.class)
                .login(userBean)
                .compose(RetrofitHelper.schedulersTransformer())
                .subscribe(new RxObserver<>() {
                    @Override
                    public void success(UserBean baseBean) {
                        httpListener.success(baseBean);
                    }

                    @Override
                    public void error(int code, String error) {
                        httpListener.error(code,error);
                    }
                });
    }
    public static void register( UserBean userBean, HttpListener<UserBean> httpListener){
        Log.d("Tag","Register start");
        new RetrofitHelper().createApi(HttpApiService.class)
                .register(userBean)
                .compose(RetrofitHelper.schedulersTransformer())
                .subscribe(new RxObserver<>() {
                    @Override
                    public void success(UserBean baseBean) {
                        httpListener.success(baseBean);
                    }

                    @Override
                    public void error(int code, String error) {
                        httpListener.error(code,error);
                    }
                });
    }
    public static void selectAll(String token, int id, HttpListener<ArrayList<UserBean>> httpListener){
        Log.d("Tag","user selectAll start");
        new RetrofitHelper().createApi(HttpApiService.class)
                .selectAllUser(token,id)
                .compose(RetrofitHelper.schedulersTransformer())
                .subscribe(new RxObserver<>() {
                    @Override
                    public void success(ArrayList<UserBean> baseBean) {
                        httpListener.success(baseBean);
                    }

                    @Override
                    public void error(int code, String error) {
                        httpListener.error(code,error);
                    }
                });
    }
    public static void selectById(String token, int id, HttpListener<UserBean> httpListener){
        Log.d("Tag","user selectById start");
        new RetrofitHelper().createApi(HttpApiService.class)
                .UserSelectById(token,id)
                .compose(RetrofitHelper.schedulersTransformer())
                .subscribe(new RxObserver<>() {
                    @Override
                    public void success(UserBean baseBean) {
                        httpListener.success(baseBean);
                    }

                    @Override
                    public void error(int code, String error) {
                        httpListener.error(code,error);
                    }
                });
    }
    public static void update(String token, UserBean userBean, HttpListener<String> httpListener){
        Log.d("Tag","userUpdate start");
        new RetrofitHelper().createApi(HttpApiService.class)
                .updateUser(token,userBean)
                .compose(RetrofitHelper.schedulersTransformer())
                .subscribe(new RxObserver<>() {
                    @Override
                    public void success(String data) {
                        httpListener.success(data);
                    }

                    @Override
                    public void error(int code, String error) {
                        httpListener.error(code,error);
                    }
                });
    }
    public static void updatePwd(String token, UserBean userBean, HttpListener<String> httpListener){
        Log.d("Tag","updatePwd start");
        new RetrofitHelper().createApi(HttpApiService.class)
                .updatePassword(token,userBean)
                .compose(RetrofitHelper.schedulersTransformer())
                .subscribe(new RxObserver<>() {
                    @Override
                    public void success(String data) {
                        httpListener.success(data);
                    }

                    @Override
                    public void error(int code, String error) {
                        httpListener.error(code,error);
                    }
                });
    }
}
